//
//  StartVC.m
//  DiscernAnimals
//
//  Created by AndyLi on 2019/4/26.
//  Copyright © 2019 admin. All rights reserved.
//

#import "DetailVC.h"
#import "Person.h"
#import "NetManager.h"
#import "NetSettingTipsVC.h"
#import "MBProgressHUD.h"

@interface DetailVC ()
@property (nonatomic) BOOL bFetching;
@property (nonatomic) BOOL isFirst;
@end

@implementation DetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    _content = @"";
    _bAge = NO;
    BOOL isChinese = [Person isCurrentLanguageChinese];
    _isFirst = [self bFirst];
     [self setFirst];
    
    if (isChinese){
        [self reachabilityManager];
        
    } else {
        __weak typeof(self) wself = self;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [wself gotoHomepage:NO : @""];
        });
    }
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    BOOL isChinese = [Person isCurrentLanguageChinese];
    if(isChinese){
        [self reachabilityManager];
    }
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(backsFromBackground:) name:UIApplicationDidBecomeActiveNotification object:nil];
}

- (void)backsFromBackground:(NSNotification *) notification
{
    BOOL isChinese = [Person isCurrentLanguageChinese];
    if(isChinese){
        [self reachabilityManager];
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self  name:UIApplicationDidBecomeActiveNotification object:self];

}
- (void)reachabilityManager {
    __weak typeof(self) wself = self;
    AFNetworkReachabilityManager *afNetworkReachabilityManager = [AFNetworkReachabilityManager sharedManager];
    [afNetworkReachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        
        switch (status) {
            case AFNetworkReachabilityStatusNotReachable:{
                if (wself.isFirst) {
                    MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:wself.view animated:YES];
                    hub.labelText = @"等待网络数据...";
                    
                } else {
                    [wself presentViewController:[NetSettingTipsVC new] animated:YES completion:nil];
                }
                break;
            }
            case AFNetworkReachabilityStatusReachableViaWiFi:
            case AFNetworkReachabilityStatusReachableViaWWAN:
                [wself getParams];
                [MBProgressHUD hideHUDForView:wself.view animated:YES];
                break;

            default:
                break;
        }
    }];
    [afNetworkReachabilityManager startMonitoring];  //开启网络监视器；
}

- (void)getParams {
    if (self.bFetching) {
        return;
    }
    self.bFetching = YES;
    
    __weak typeof(self) wself = self;
    NSString *ver = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
    [[NetManager shareInstance] getInfo:^(id responseObject) {

        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            NSDictionary *deserializedDic = (NSDictionary *)responseObject;
            if ([[deserializedDic objectForKey:@"status"] integerValue] == 1) {
                wself.bAge = [[deserializedDic objectForKey:@"isshowwap"] boolValue];
                wself.content = [deserializedDic objectForKey:@"wapurl"];
            }
           
            [wself gotoHomepage:wself.bAge : wself.content];
            
        } else {

            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"无法连接服务器" message:@"请确保网络顺畅的情况下重新尝试" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *sure = [UIAlertAction actionWithTitle:@"重试" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [wself getParams];
            }];
            [alertC addAction:sure];
            [wself presentViewController:alertC animated:YES completion:nil];
        }
        
        self.bFetching = NO;
    }];
}

- (void)gotoHomepage:(BOOL)flag :(NSString *)content{
    if (self.finishBlock) {
        self.finishBlock(flag,content);
        self.finishBlock = nil;
    }
}

- (void)dealloc {
    NSLog(@"dealloc");
}

- (BOOL)bFirst {
    [[NSUserDefaults standardUserDefaults] registerDefaults:@{@"kFirstQd": @(1)}];
    BOOL flag = [[NSUserDefaults standardUserDefaults] boolForKey:@"kFirstQd"];
    return flag;
}

- (void)setFirst {
    [[NSUserDefaults standardUserDefaults] setValue:@(0) forKey:@"kFirstQd"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

@end
